"""Promethues监控指标"""
from common.package.elastic_db import ElasticsearchDB


class ElasticsearchMetric(ElasticsearchDB):

    def metric(self):
        health = self.client.cat.health(format='json')
        health = health[0]

        allocation = self.client.cat.allocation(bytes='b', format='json')
        indices = self.client.cat.indices(bytes='b', format='json')

        return {
            'node_count': int(health['node.total']),
            'shards': int(health['shards']),
            'primary_shards': int(health['pri']),
            'unassign': int(health['unassign']),
            'pending_tasks': int(health['unassign']),
            'epoch': int(health['epoch']),
            'cluster': health['cluster'],
            'allocation': [{'host': item['host'],
                            'node': item['node'],
                            'total': int(item['disk.total']),
                            'used': int(item['disk.used']),
                            'avail': int(item['disk.avail']),
                            'shards': int(item['shards'])
                            }
                           for item in allocation if item.get('disk.avail')],
            'indices': [{
                'count': int(item['docs.count']),
                'deleted': int(item['docs.deleted']),
                'size': int(item['store.size']),
                'index': item['index'],
                'status': item['status'],
                'health': item['health'],
            } for item in indices]
        }


if __name__ == '__main__':
    elasticsearch_metric = ElasticsearchMetric(hosts='http://192.168.101.79:9200')
    a = elasticsearch_metric.metric()

    print(a)
